package com.example.demo.controller;


import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhuzhongbiao on 2017/3/29.
 */
@RestController
@RequestMapping("/s")
public class StudentController {

    @Autowired
    private IUserService userService;


    @GetMapping("/add")
    public Object addUser() throws JSONException {
        User user = new User();
        user.setUsername("李四");
        user.setAge(12);
        JSONObject result = new JSONObject();
        result.put("result", userService.insert(user));
        return result;
    }


    @GetMapping("/get")
    public Object get(@RequestParam(required = true) int id)  {
        User user  = userService.selectById(id);
        return user;
    }


    /***
     *
     *
     boolean update(T var1, Wrapper<T> var2);
     *
     *
     * @param id
     * @return
     */
    @GetMapping("/update")
    public Object update(int id)  {
        User user  = new User();
        user.setId(2);
        user.setUsername("王五");
        boolean row  = userService.updateById(user);
        return row;
    }


    @GetMapping("/delete")
    public Object delete(int id)  {
        boolean row  = userService.deleteById(2);
        return row;
    }




}
